unit frmusers;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  ExtCtrls, Buttons;

type

  { TUsersForm }

  TUsersForm = class(TForm)
    AddBitBtn: TBitBtn;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    EditBitBtn: TBitBtn;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    procedure AddBitBtnClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure EditBitBtnClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  UsersForm: TUsersForm;

implementation
 uses udbsettings, frmuseredit;
{$R *.lfm}

 { TUsersForm }

 procedure TUsersForm.AddBitBtnClick(Sender: TObject);
 begin
  UserForm.NameEdit.Clear;
  UserForm.PasswordEdit.Clear;
  frmuseredit.isOK:=false;
  UserForm.ShowModal;
  if frmuseredit.isOk then begin;
   //TODO: правильний виклик процедури на сервері
   DBSettingsForm.SQLQuery1.Close;
   DBSettingsForm.SQLQuery1.SQL.Clear;
   DBSettingsForm.SQLQuery1.SQL.Add('execute procedure users_procedure(:par_id, :par_username, :par_pass);');
   DBSettingsForm.SQLQuery1.Params[0].AsInteger:=-1;
   DBSettingsForm.SQLQuery1.Params[1].AsString:=UserForm.NameEdit.Text;
   DBSettingsForm.SQLQuery1.Params[2].AsString:=UserForm.PasswordEdit.Text;
   DBSettingsForm.SQLQuery1.ExecSQL;
   DBSettingsForm.SQLTransaction1.Commit;
   DBSettingsForm.SQLQuery1.Active:=false;
   DBSettingsForm.SQLQuery1.SQL.Clear;
   DBSettingsForm.SQLQuery1.SQL.Add('SELECT ID, username, pass from users');
   DBSettingsForm.SQLQuery1.Open;
  end;
 end;

procedure TUsersForm.BitBtn1Click(Sender: TObject);
begin
  DBSettingsForm.SQLQuery1.Delete;
  DBSettingsForm.SQLQuery1.ApplyUpdates;
end;

procedure TUsersForm.BitBtn2Click(Sender: TObject);
begin
  DBSettingsForm.SQLQuery1.ApplyUpdates;
  DBSettingsForm.SQLTransaction1.Commit;
  Close;
end;

procedure TUsersForm.BitBtn3Click(Sender: TObject);
begin
  DBSettingsForm.SQLTransaction1.Rollback;
  Close;
end;

procedure TUsersForm.EditBitBtnClick(Sender: TObject);
var oldid : Integer;
begin
 oldid:=DBSettingsForm.SQLQuery1.FieldByName('ID').AsInteger;
 UserForm.NameEdit.Text:=DBSettingsForm.SQLQuery1.FieldByName('USERNAME').AsString;
 UserForm.PasswordEdit.Text:=DBSettingsForm.SQLQuery1.FieldByName('PASS').AsString;
 frmuseredit.isOK:=false;
 UserForm.ShowModal;
 if frmuseredit.isOk then begin;
  //виклик процедури на сервері
  DBSettingsForm.SQLQuery1.Close;
  DBSettingsForm.SQLQuery1.SQL.Clear;
  DBSettingsForm.SQLQuery1.SQL.Add('execute procedure users_procedure(:par_id, :par_username, :par_pass);');
  DBSettingsForm.SQLQuery1.Params[0].AsInteger:=oldid;
  DBSettingsForm.SQLQuery1.Params[1].AsString:=UserForm.NameEdit.Text;
  DBSettingsForm.SQLQuery1.Params[2].AsString:=UserForm.PasswordEdit.Text;
  DBSettingsForm.SQLQuery1.ExecSQL;
  DBSettingsForm.SQLTransaction1.Commit;
  DBSettingsForm.SQLQuery1.Active:=false;
  DBSettingsForm.SQLQuery1.SQL.Clear;
  DBSettingsForm.SQLQuery1.SQL.Add('SELECT ID, username, pass from users');
  DBSettingsForm.SQLQuery1.Open;
 end;
end;


end.

